package com.wpx.batch.reader;

import com.wpx.batch.ExecuteContext;
import com.wpx.batch.ItemReader;
import com.wpx.batch.WpxBatchUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 抽象的阻塞读
 */
public abstract class AbstractBlockItemReader<E> implements ItemReader<E> {

    @Override
    public List<E> read(ExecuteContext executeContext) {
        List<E> data;
        while (true){
            if(executeContext.getJob().isWillStop()){
                return new ArrayList<>(); //作业将结束 返回空集合
            }
            data = readDatas(executeContext);
            if(data.size() > 0){
                break;
            }else{
                WpxBatchUtils.sleep(500);  //一直没数据 等待500毫秒 再去读
            }
        }
        return data;
    }

    public abstract List<E> readDatas(ExecuteContext executeContext);
}
